package com.shuoyue.ycgk.ui.mine.learnprogress;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.chad.library.adapter.base.BaseViewHolder;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.baseadapter.AppBaseQuickAdapter;
import com.shuoyue.ycgk.utils.XDateUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DealCalenderAdapter extends AppBaseQuickAdapter<DayInfo> {
    Calendar calendar;
    Calendar calendarStart;
    Calendar calendarEnd;

    public DealCalenderAdapter(@Nullable List<DayInfo> data) {
        super(R.layout.item_calendar, data);
        calendar = Calendar.getInstance();
    }

    @Override
    protected void convert(BaseViewHolder helper, DayInfo item) {
        helper.addOnClickListener(R.id.text);
        helper.setText(R.id.text, (item.getDayInMouth()) + "");
        Calendar calendarItem = Calendar.getInstance();
        calendarItem.setTime(item.getData());
        //初始化，后面的根据条件转
        helper.getView(R.id.text).setBackground(null);
        helper.setTextColor(R.id.text, ContextCompat.getColor(mContext, R.color.all_text_gray_color));
//        helper.setBackgroundColor(R.id.lay_all, ContextCompat.getColor(mContext,item.getIsDone() == 1
//                ? R.color.option_bg_green
//                : R.color.white));
        helper.setGone(R.id.doneTag, item.getIsDone() == 1);
        if (calendarStart != null
                && calendarStart.get(Calendar.YEAR) == calendarItem.get(Calendar.YEAR)
                && calendarStart.get(Calendar.MONTH) == calendarItem.get(Calendar.MONTH)
                && calendarStart.get(Calendar.DAY_OF_MONTH) == calendarItem.get(Calendar.DAY_OF_MONTH)) {
            //起始位置
            helper.setBackgroundRes(R.id.text, R.drawable.bg_mok_circular_red);
            helper.setTextColor(R.id.text, ContextCompat.getColor(mContext, R.color.white));
        }
        if (calendarEnd != null
                && calendarEnd.get(Calendar.YEAR) == calendarItem.get(Calendar.YEAR)
                && calendarEnd.get(Calendar.MONTH) == calendarItem.get(Calendar.MONTH)
                && calendarEnd.get(Calendar.DAY_OF_MONTH) == calendarItem.get(Calendar.DAY_OF_MONTH)) {
            //起始位置
            helper.setBackgroundRes(R.id.text, R.drawable.bg_mok_circular_red);
            helper.setTextColor(R.id.text, ContextCompat.getColor(mContext, R.color.white));
        }
        if (item.isSelect) {
            helper.setBackgroundRes(R.id.text, R.drawable.bg_circular_blue);
            helper.setTextColor(R.id.text, ContextCompat.getColor(mContext, R.color.white));
        }
    }

    public Calendar getCalendarStart() {
        return calendarStart;
    }

    public Calendar getCalendarEnd() {
        return calendarEnd;
    }

    public void setStartDate(String dateStr) {
        try {
            Date date = XDateUtils.cover(dateStr, "yyyy-MM-dd");
            calendarStart = Calendar.getInstance();
            calendarStart.setTime(date);
            notifyDataSetChanged();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setEndDate(String dateStr) {
        try {
            Date date = XDateUtils.cover(dateStr, "yyyy-MM-dd");
            calendarEnd = Calendar.getInstance();
            calendarEnd.setTime(date);
            notifyDataSetChanged();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
